<!-- 公共文件 start -->
@extends('admin.public.base')
<!-- 公共文件 end -->

@section('content')
    <div class="layui-fluid">
        <div class="layui-row">
            <form class="layui-form layui-form-pane">
                <div class="layui-form-item">
                    <label for="L_role_name" class="layui-form-label">
                        <span class="x-red">*</span>角色名
                    </label>
                    <div class="layui-input-inline">
                        <input type="text" name="name" value="{{ $data['name'] }}" lay-verify="required"
                               lay-reqtext="必填项,请填写信息" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label for="L_sort" class="layui-form-label">排序</label>
                    <div class="layui-input-inline">
                        <input type="text" name="sort" value="{{ $data['sort'] }}" class="layui-input">
                    </div>
                    <div class="layui-form-mid layui-word-aux">数字：1~99，越小优先级越高</div>
                </div>

                <div class="layui-form-item layui-form-text">
                    <label class="layui-form-label">
                        拥有权限
                    </label>
                    <table class="layui-table layui-input-block">
                        <tbody>
                        @foreach($menuList as $vv1)
                            <tr>
                                <td>
                                    <input type="checkbox" name="auth[]" lay-skin="primary" lay-filter="father"
                                           value="{{$vv1['id']}}" title="{{$vv1['name']}}" @if($vv1['is_select'] ==1) checked @endif>
                                </td>
                                <td>
                                    <div class="layui-input-block">
                                        @foreach($vv1['son'] as $vv2)
                                            <input name="auth[]" lay-skin="primary" type="checkbox"
                                                   value="{{$vv2['id']}}" title="{{$vv2['name']}}" @if($vv2['is_select'] ==1) checked @endif>
                                        @endforeach
                                    </div>
                                </td>
                            </tr>
                        @endforeach
                        </tbody>
                    </table>
                </div>
                <div class="layui-form-item">
                    <label for="L_note" class="layui-form-label">备注</label>
                    <div class="layui-input-inline">
                        <input type="text" name="note" value="{{ $data['note'] }}" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <input type="hidden" value="{{ $data['id'] }}" name="id">
                    <button class="layui-btn" lay-submit="" lay-filter="add">提交</button>
                </div>
            </form>
        </div>
    </div>
    <script>

        layui.use(['form', 'layer', 'jquery'], function () {
            var $ = layui.jquery;
            var form = layui.form,
                layer = layui.layer;

            //自定义验证规则
            form.verify({
                role_name: function (value) {
                    if (value.length < 2 || value.length > 16) {
                        return '角色名必须为2到16的字符';
                    }
                }
            });
            form.on('checkbox(father)', function (data) {

                if (data.elem.checked) {
                    $(data.elem).parent().siblings('td').find('input').prop("checked", true);
                    form.render();
                } else {
                    $(data.elem).parent().siblings('td').find('input').prop("checked", false);
                    form.render();
                }
            });

            //监听提交
            form.on('submit(add)', function (data) {
                console.log(data);
                //发异步，把数据提交给php
                $.ajax({
                    type: 'POST',
                    url: '/admin/system/roleEdit',
                    data: data.field,
                    dataType: 'json',
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
                    },
                    success: function (data) {
                        // console.log(data);
                        if (data.code == 1) {
                            layer.alert("更新成功", {
                                    icon: 6
                                },
                                function () {
                                    //关闭当前frame
                                    xadmin.close();

                                    // 可以对父窗口进行刷新
                                    xadmin.father_reload();
                                });
                        } else {
                            layer.alert(data.msg)
                        }
                    },
                    error: function (xhr, type) {
                        alert('Ajax error!')
                    }
                });

                return false;
            });

        });
    </script>
    <script>
        layui.use('laydate',
            function () {
                var laydate = layui.laydate;

                //执行一个laydate实例
                laydate.render({
                    elem: '#start', //指定元素
                    type: 'datetime'
                });

                //执行一个laydate实例
                laydate.render({
                    elem: '#end', //指定元素
                    type: 'datetime'
                });
            });

    </script>

@endsection
